package com.foolishbird.sdk.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.foolishbird.sdk.params.bean.VerifyType;
import com.foolishbird.sdk.ui.activity.UserLoginActivity;
import com.foolishbird.sdk.ui.view.DialogFactory;
import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.ToolsUtils;
import com.foolishbird.sdk.utils.WL;


public class RealnameVerifyWithPhoneFragment extends PhoneVerifyFragment implements View.OnClickListener, TextWatcher {

    private EditText mIdentificationNameEt;
    private EditText mIdentificationCardEt;
    private EditText mBindPhoneNumberEt;
    private EditText mVerifyCodeEt;
    private Button mSendVerifyCodeBtn;

    private Button mBindPhoneBtn;

    public static RealnameVerifyWithPhoneFragment newInstance(Bundle bundle) {
        RealnameVerifyWithPhoneFragment fragment = new RealnameVerifyWithPhoneFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        WL.d(".....","onAttack");
        if (activity instanceof UserLoginActivity) {
            ((UserLoginActivity) activity).setOnPhoneVerifyCodeListener(this);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "hnssn_fragment_realname_verify_phone");
        View contentView = inflater.inflate(resource, null);

        mIdentificationNameEt = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "activity_bind_identification_name"));
        mIdentificationCardEt = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "activity_bind_identification_card"));
        mBindPhoneBtn = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "activity_bindphone_submit_btn"));
        mBindPhoneNumberEt = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "activity_bindphone_number_et"));
        mVerifyCodeEt = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "activity_bindphone_verify_code_et"));
        mSendVerifyCodeBtn = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "activity_bindphone_send_verify_btn"));
        mBindPhoneNumberEt.addTextChangedListener(this);
        mVerifyCodeEt.addTextChangedListener(this);
        mSendVerifyCodeBtn.setOnClickListener(this);
        mBindPhoneNumberEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int keyCode, KeyEvent event) {
                if(keyCode ==  EditorInfo.IME_ACTION_NEXT&&getSendVerifyCodeView().isClickable()){
                    sendPhoneVerifyCode();
                }
                Log.e("OnKeyListener",keyCode+".."+(event==null));
                return false;
            }
        });
        mVerifyCodeEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_DONE&&mBindPhoneBtn.isEnabled()){
                    doBindIdentityCardNumber();
                }
                Log.e("CodeEtOnKeyListener",actionId+".."+(event==null));
                return false;
            }
        });

        mBindPhoneBtn.setOnClickListener(this);

        mIdentificationNameEt.addTextChangedListener(this);
        mIdentificationCardEt.addTextChangedListener(this);
        return contentView;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        checkSumbitButtonState();
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onClick(View v) {
        if (v == mSendVerifyCodeBtn) {
            sendPhoneVerifyCode();
        }else {
            doBindIdentityCardNumber();
        }

    }

    private void checkSumbitButtonState() {
        String realName = mIdentificationNameEt.getText().toString().trim();
        String idCardNumber = mIdentificationCardEt.getText().toString().trim();
        String phone = mBindPhoneNumberEt.getText().toString().trim();
        String code = mVerifyCodeEt.getText().toString().trim();

        if (TextUtils.isEmpty(realName) || TextUtils.isEmpty(idCardNumber)||TextUtils.isEmpty(phone)||TextUtils.isEmpty(code)) {
            mBindPhoneBtn.setEnabled(false);
        } else {
            mBindPhoneBtn.setEnabled(true);
        }
    }

    private void doBindIdentityCardNumber() {
        String realName = mIdentificationNameEt.getText().toString().trim();
        String idCardNumber = mIdentificationCardEt.getText().toString().trim();
        String phoneNumber = mBindPhoneNumberEt.getText().toString().trim();
        String phoneCode = mVerifyCodeEt.getText().toString().trim();

        if (TextUtils.isEmpty(realName)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_input_realname"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        if (TextUtils.isEmpty(idCardNumber)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_idcard_number"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        if (!ToolsUtils.checkIdentityCardNumber(idCardNumber)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_idcard_correct"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        if(TextUtils.isEmpty(phoneNumber)){
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_telephonenumber"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        if (!ToolsUtils.checkCellphone(phoneNumber)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_phonecurrent"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        if (!ToolsUtils.checkNumber(phoneCode)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_captcha"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        FoolishBirdBindDialogFragment.OnRealNameVerifyListener listener = getOnRealNameVerifyListener();
        if (listener != null) {
            listener.doBindOrRealNameVerify(phoneNumber, phoneCode, realName, idCardNumber);
        }
    }

    private FoolishBirdBindDialogFragment.OnRealNameVerifyListener getOnRealNameVerifyListener() {
        Activity activity = getActivity();
        if (activity instanceof FoolishBirdBindDialogFragment.OnRealNameVerifyListener) {
            return (FoolishBirdBindDialogFragment.OnRealNameVerifyListener) activity;
        }
        return null;
    }

    @Override
    protected Button getSendVerifyCodeView() {
        return  mSendVerifyCodeBtn;
    }

    @Override
    protected EditText getEditorVerifyCodeView() {
        return mVerifyCodeEt;
    }

    private void sendPhoneVerifyCode() {
        String phoneNumber = mBindPhoneNumberEt.getText().toString().trim();

        if (TextUtils.isEmpty(phoneNumber)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_telephonenumber"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        if (!ToolsUtils.checkCellphone(phoneNumber)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_phonecurrent"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }
        if (getActivity() instanceof UserLoginActivity) {
            ((UserLoginActivity) getActivity()).setOnPhoneVerifyCodeListener(this);
        }
        requestVerifyCode(phoneNumber, VerifyType.PHONE_VERIFY);
    }
}
